
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        List<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        //第一种遍历方式
        System.out.println(list);
        //第2种遍历方式:for
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");//获得i下标的元素并输出
        }
        System.out.println();
        //第3种遍历方式:for-each
        for (int x: list) {
            System.out.print(x+" ");
        }
        //第4种遍历方式:Iterator
        System.out.println();
        Iterator iterator= list.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next()+" ");
        }
        System.out.println();
        //第5种遍历方式ListIterator
        ListIterator it= list.listIterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        //第6种遍历方式:从后往前遍历
        ListIterator iterator1=list.listIterator(list.size());
        while (iterator1.hasPrevious()){
            System.out.print(iterator1.previous()+" ");
        }
    }
}
